package com.ruoyi.algorithm.service.impl;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.algorithm.cross.block.PageListBlock;
import com.ruoyi.algorithm.cross.fallback.MinIOFallback;
import com.ruoyi.algorithm.cross.feign.MinIOFeign;
import com.ruoyi.algorithm.entity.DemoEntity;
import com.ruoyi.algorithm.repository.DemoRepository;
import com.ruoyi.algorithm.service.DemoService;
import com.beihu.sightplus.common.request.PageRequest;
import com.beihu.sightplus.common.response.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Service
public class DemoServiceImpl implements DemoService {
    @Autowired
    private DemoRepository demoRepository;

    @Autowired
    private MinIOFeign minIOFeign;

    @Override
    @SentinelResource(value = "page", blockHandlerClass = PageListBlock.class, blockHandler = "pageList")
    public List<DemoEntity> pageList(PageRequest pageRequest) {

//        throw new RuntimeException();

        // 业务代码1
        // 业务代码2
        List<DemoEntity> demoList = demoRepository.list();


        return demoList;
    }

    /**
     * 上传文件
     */
    @SentinelResource(value = "pic", fallbackClass = MinIOFallback.class, fallback = "uploadPicFallback")
    public ResultData uploadPic(MultipartFile file) {

        String path = "cat";
        String url = minIOFeign.uploadFile(file, path);
        ResultData resultData = JSONObject.parseObject(url, ResultData.class);
        return resultData;
    }

}
